﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace LenderClearingHouse
{
  public partial class UserManagement : PageBase
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      hfUserId.Value = CurrentUser.UserId;
      pnlNotifications.Visible = CurrentUser.UserRole == UserRole.SuperAdmin;

      if (!IsPostBack)
      {        
        DataAccess da = new DataAccess();
        DataTable tbl = da.ExecuteSQL("select * from Config");

        foreach (DataRow dr in tbl.Rows)
        {
          if (dr["Key"].ToString() == "LvreoNotificationList")
          {
            tbLvreoNotification.Text = dr["Value"].ToString();
          }
          else if (dr["Key"].ToString() == "SdreoNotificationList")
          {
            tbSdreoNotification.Text = dr["Value"].ToString();
          }
        }
      }
    }

    protected void grid_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
      GridEditableItem editedItem = e.Item as GridEditableItem;
      EditUserControl userControl =
        (EditUserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);

      string firstName = (userControl.FindControl("tbFirstName") as TextBox).Text;
      string lastName = (userControl.FindControl("tbLastName") as TextBox).Text;
      string email = (userControl.FindControl("tbEmail") as TextBox).Text;
      string password = (userControl.FindControl("tbPassword") as TextBox).Attributes["value"];
      bool isVIP = (userControl.FindControl("cbVIP") as CheckBox).Checked;

      string role = "SA";
      string subRole = "N";
      string currentUserId = CurrentUser.UserId;

      switch (userControl.Role)
      {
        case UserRole.SuperAdmin: role = "SA"; break;
        case UserRole.InvestorAdmin:
          role = "IA";
          subRole = (userControl.FindControl("ddlInvestorAdminRole") as DropDownList).SelectedValue;
          break;
        case UserRole.InvestorUser:
          role = "IU";
          subRole = (userControl.FindControl("ddlInvestorRole") as DropDownList).SelectedValue;
          if (CurrentUser.UserRole == UserRole.SuperAdmin)
          {
            currentUserId = (userControl.FindControl("ddlInvestorAdmin") as DropDownList).SelectedValue;
          }
          break;
        case UserRole.User: role = "US"; break;
        case UserRole.EscrowUser: role = "EU"; break;
        case UserRole.ProjectManager: role = "PM"; break;
      }      

      string lvreo = (userControl.FindControl("ddlLvreoRole") as DropDownList).SelectedValue;
      string sdreo = (userControl.FindControl("ddlSdreoRole") as DropDownList).SelectedValue;
      string masterlist = (userControl.FindControl("ddlMasterlist") as DropDownList).SelectedValue;

      DataAccess da = new DataAccess();
      da.ExecuteProcedure("createUser",
        new string[] { "@FirstName", "@LastName", "@Email", "@Password", "@LvreoRole",
          "@SdreoRole", "@MasterListRole", "@Role", "@SubRole", "@CreatedByUser", "@isVIP" },
        new object[] { firstName, lastName, email, password, lvreo, sdreo, masterlist,
          role, subRole, currentUserId, isVIP});
    }

    protected void grid_UpdateCommand(object sender, GridCommandEventArgs e)
    {
      GridEditableItem editedItem = e.Item as GridEditableItem;
      EditUserControl userControl =
        (EditUserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);

      int userId = int.Parse((userControl.FindControl("hfUserId") as HiddenField).Value);
      string firstName = (userControl.FindControl("tbFirstName") as TextBox).Text;
      string lastName = (userControl.FindControl("tbLastName") as TextBox).Text;
      string email = (userControl.FindControl("tbEmail") as TextBox).Text;
      bool active = (userControl.FindControl("cbActive") as CheckBox).Checked;      
      string password = (userControl.FindControl("tbPassword") as TextBox).Attributes["value"];
      bool isVIP = (userControl.FindControl("cbVIP") as CheckBox).Checked;
      string lvreo = (userControl.FindControl("ddlLvreoRole") as DropDownList).SelectedValue;
      string sdreo = (userControl.FindControl("ddlSdreoRole") as DropDownList).SelectedValue;
      string masterlist = (userControl.FindControl("ddlMasterlist") as DropDownList).SelectedValue;

      string role = "SA";
      string subRole = "N";
      string currentUserId = CurrentUser.UserId;

      switch (userControl.Role)
      {
        case UserRole.SuperAdmin: role = "SA"; break;
        case UserRole.InvestorAdmin:
          role = "IA";
          subRole = (userControl.FindControl("ddlInvestorAdminRole") as DropDownList).SelectedValue;
          break;
        case UserRole.InvestorUser:
          role = "IU";
          subRole = (userControl.FindControl("ddlInvestorRole") as DropDownList).SelectedValue;
          if (CurrentUser.UserRole == UserRole.SuperAdmin)
          {
            currentUserId = (userControl.FindControl("ddlInvestorAdmin") as DropDownList).SelectedValue;
          }
          break;
        case UserRole.User: role = "US"; break;
        case UserRole.EscrowUser: role = "EU"; break;
        case UserRole.ProjectManager: role = "PM"; break;
      }

      DataAccess da = new DataAccess();
      da.ExecuteProcedure("updateManageUser",
        new string[] { "@UserId", "@FirstName", "@LastName", "@Email", "@Password", "@Active", "@LvreoRole", "@SdreoRole", "@MasterListRole", "@Role", "@SubRole", "@createdByUser", "@isVIP" },
        new object[] { userId, firstName, lastName, email, password, active, lvreo, sdreo, masterlist, role, subRole, currentUserId, isVIP });
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
      DataAccess da = new DataAccess();

      da.ExecuteSQLNonQuery(
        string.Format("update Config set [Value] = '{0}' where [Key] = 'LvreoNotificationList'",
        tbLvreoNotification.Text));

      da.ExecuteSQLNonQuery(
        string.Format("update Config set [Value] = '{0}' where [Key] = 'SdreoNotificationList'",
        tbSdreoNotification.Text));
    }

    protected void lnkExportVIP_Click(object sender, EventArgs e)
    {
      DataAccess da = new DataAccess();
      DataTable dt = da.ExecuteSQL("getVIPUsers");
      int startRow = 1;
      Utilities.GridToExcel(dt, new string[] { "Fname", "LName", "Email" }, "UserVIP", true, ref startRow);
    }
  }
}